**************************************
*calculate household assets index(HAI)
**************************************

version 16.0

set scheme lean2
cd "${mypath}\CHNS_project\"

log using "${mypath}\CHNS_project\03_log_files\02_calculate_HAI.log", replace

local path1 "01_data\01_CHNS\"
local path2 "01_data\02_posted\"
import sas using "`path1'\asset_12",clear
gen homeid=string(hhid ,"%20.0f")+string(WAVE)
destring homeid,replace
format homeid %20.0f

*generate total area of house 
recode L16 (-99 = .),gen(total_area)
gen count = 1
bys homeid:egen dipli = total(count)
drop if dipli > 1 & mi(total_area)

*generate master ID
preserve
keep homeid
sa "`path2'asset_master.dta",replace
restore

*water supply(option 5 seems to be the wrong encoding)
recode L1 (1/2 = 1 "have a tap water supply")(3/5 = 0 "lack of a tap water supply"),gen(water_supply)
la var water_supply "tap water supply"
la val water_supply water_supply

*clean toilet
recode L5 (0 3/8= 0 "lack of clean private toilet")(1/2 = 1 "have a clean private toilet"),gen(toilet)
la var toilet "clean private toilet"
la val toilet toilet

*electrical lighting
recode L7 (1 = 1 "have electrical lighting")(2/5 = 0 "lack of electrical lighting"),gen(lighting)
la var lighting "electrical lighting"
la val lighting lighting

*clean fuel
replace L8_1 = L8_2 if mi(L8_1) & !mi(L8_2) & L8_2 != 0
recode L8_1 (1 3 6/8= 0 "lack of clean fuel for cooking")(2 4 5= 1 "have clean fuel for cooking"),gen(fuel)
la var fuel "clean fuel for cooking"
la val fuel fuel

*house ownership
gen ownership = 1 if L9 == 4 & WAVE < 2009
replace ownership = 0 if L9 != 4 & L9 != 9 & !mi(L9)
replace ownership = 1 if L200 == 1 & WAVE >= 2009
replace ownership = 0 if L200 == 0 & WAVE >= 2009
la var ownership "house ownership"
la def ownership 1 "have house ownership" 0 "don't have house ownership"
la val ownership ownership

*TV
recode L100 (1 = 1 "have a black and white TV")(0 = 0 "don't have a black and white TV"),gen(BW_TV)
recode L105 (1 = 1 "have a color TV")(0 = 0 "don't have a color TV")(9=.),gen(C_TV)
gen has_TV = 1 if BW_TV == 1 | C_TV == 1
replace has_TV = 0 if BW_TV == 0 & C_TV == 0
replace has_TV = 0 if mi(has_TV) & C_TV == 0
replace has_TV = 1 if L101 > 0 & !mi(L101) & WAVE <= 2006
replace has_TV = 1 if L102 > 0 & !mi(L102) & WAVE <= 2006
replace has_TV = 1 if L106 > 0 & !mi(L106)
replace has_TV = 1 if L107 > 0 & !mi(L107)
la var has_TV "TV"
la def has_TV 1 "at least have a TV" 0 "don't have any TV"
la val has_TV has_TV

*washing machine
recode L110 (1 = 1 "at least have a washing machine")(0 = 0 "don't have any washing machine")(9 = .),gen(washing_machine)
replace washing_machine = 1 if mi(washing_machine) & ((L111 > 0 & !mi(L111))|(L112 > 0 & !mi(L112)))
la var washing_machine "washing machine"
la val washing_machine washing_machine

*refrigerator
recode L115 (1 = 1 "at least have a refrigerator")(0 = 0 "don't have any refrigerator")(9 = .),gen(refrigerator)
replace refrigerator = 1 if mi(refrigerator) & ((L116 > 0 & !mi(L116))|(L117 > 0 & !mi(L117)))
la var refrigerator "refrigerator"
la val refrigerator refrigerator

*air conditioning
recode L120 (1 = 1 "at least have an air conditioning")(0 = 0 "don't have any air conditioning")(9 = .),gen(air)
replace air = 1 if mi(air) & ((L121 > 0 & !mi(L121))|(L122 > 0 & !mi(L122)))
la var air "air conditioning"
la val air air 

*sewing machine
recode L125 (1 = 1 "at least have a sewing machine")(0 = 0 "don't have any sewing machine")(9 = .),gen(sewing)
replace sewing = 1 if mi(sewing) & ((L126 > 0 & !mi(L126))|(L127 > 0 & !mi(L127)))
la var sewing "sewing machine"
la val sewing sewing

*electric fans
recode L130 (1 = 1 "at least have an electric fans")(0 = 0 "don't have any electric fans")(9 = .),gen(fans)
replace fans = 1 if mi(fans) & ((L131 > 0 & !mi(L131))|(L132 > 0 & !mi(L132)))
la var fans "electric fans"
la val fans fans 

*camera
recode L140 (1 = 1 "at least have a camera")(0 = 0 "don't have any camera")(9 = .),gen(camera)
replace camera = 1 if mi(camera) & ((L141 > 0 & !mi(L141))|(L142 > 0 & !mi(L142)))
la var camera "camera"
la val camera camera 

*microwave oven
recode L140A (1 = 1 "at least have a microWAVE oven")(0 = 0 "don't have any microWAVE oven")(9 = .),gen(micro_oven)
replace micro_oven = 1 if mi(micro_oven) & ((L141A > 0 & !mi(L141A))|(L142A > 0 & !mi(L142A)))
la var micro_oven "microwave oven"
la val micro_oven micro_oven 

*rice cooker
recode L140B (1 = 1 "at least have a rice cooker")(0 = 0 "don't have any rice cooker")(9 = .),gen(rice_cook)
replace rice_cook = 1 if mi(rice_cook) & ((L141B > 0 & !mi(L141B))|(L142B > 0 & !mi(L142B)))
la var rice_cook "rice cooker"
la val rice_cook rice_cook

*pressure cooker
recode L140C (1 = 1 "at least have a pressure cooker")(0 = 0 "don't have any pressure cooker")(9=.),gen(pressure_cook)
replace pressure_cook = 1 if mi(pressure_cook) & ((L141C > 0 & !mi(L141C))|(L142C > 0 & !mi(L142C)))
la var pressure_cook "pressure cooker"
la val pressure_cook pressure_cook

*bike
recode L23 (1 = 1 "at least have a")(0 = 0 "don't have any ")(9 = .),gen(bike)
replace bike = 1 if mi(bike) & (L24 > 0 & !mi(L24))
la var bike "bike"
la val bike bike

*motorcycle
recode L27 (1 = 1 "at least have a")(0 = 0 "don't have any ")(9 = .),gen(motorcycle)
replace motorcycle = 1 if mi(motorcycle) & (L28 > 0 & !mi(L28)) 
la var motorcycle "motorcycle"
la val motorcycle motorcycle 

*car
recode L31 (1 = 1 "at least have a")(0 = 0 "don't have any ")(9 = .),gen(car)
replace car = 1 if mi(car) & (L32 > 0 & !mi(L32))
la var car "car"
la val car car


keep hhid WAVE water_supply toilet lighting fuel ownership has_TV washing_machine refrigerator ///
     air sewing fans camera micro_oven rice_cook pressure_cook bike motorcycle car

gen homeid=string(hhid ,"%20.0f")+string(WAVE)
destring homeid,replace
format homeid %20.0f

merge 1:1 homeid using "`path2'asset_master.dta",keep(match)nogen

*constructing household assets index(HAI) by principal component analysis
quiet pca water_supply toilet lighting fuel ownership has_TV washing_machine refrigerator air sewing /// 
fans camera micro_oven rice_cook pressure_cook bike motorcycle car
quiet estat kmo
quiet predict HAI,score

keep homeid HAI

sa "`path2'HAI.dta",replace
log close
